/**
 * Created by Dick on 2016/4/7.
 */
$(function () {
    "use strict";
    if (document.getElementById('upload-pic')) {
        document.getElementById('upload-pic').addEventListener('change', Selecttx, false);
    }
    if (document.getElementById('upload-bg')) {
        document.getElementById('upload-bg').addEventListener('change', Selectbg, false);
    }
    var jic = {
        compress: function (source_img_obj, size, width, height, output_format) {
            var mime_type = "image/jpeg";
            if (output_format != undefined && output_format == "png") {
                mime_type = "image/png";
            }
            var quality = 0.95;
            var maxsize = 0.8 * 1024 * 1024;
            if (size > maxsize && size <= maxsize * 1.5) {
                quality = 0.93;
            }
            if (size > maxsize * 1.5 && size <= maxsize * 2) {
                quality = 0.91;
            }
            if (size > maxsize * 2 && size <= maxsize * 3) {
                quality = 0.88;
            }
            if (size > maxsize * 3) {
                debugger;
                var num = maxsize / size;
                quality *= maxsize * (num - 1 / num) * 0.88 / size;
            }

            var cvs = document.createElement('canvas');
            if (width == 0 && height == 0) {
                width = source_img_obj.naturalWidth;
                height = source_img_obj.naturalHeight;
                if (width >= height && width > 2560) {
                    height = 2560 * height / width;
                    width = 2560;
                    if (size > maxsize * 3 && quality > 0.75) {
                        quality = 0.70;
                    }
                } else if (height >= width && height > 2560) {
                    width = 2560 * width / height;
                    height = 2560;
                } else if (width >= height && width > 1920) {
                    height = 1920 * height / width;
                    width = 1920;
                } else if (height >= width && height > 1920) {
                    width = 1920 * width / height;
                    height = 1920;
                } else if (width >= height && width > 1680) {
                    height = 1680 * height / width;
                    width = 1680;
                } else if (height >= width && height > 1680) {
                    width = 1680 * width / height;
                    height = 1680;
                } else if (width >= height && width > 1080) {
                    height = 1080 * height / width;
                    width = 1080;
                } else if (height >= width && height > 1080) {
                    width = 1080 * width / height;
                    height = 1080;
                }
                cvs.width = width;
                cvs.height = height;
            } else {
                cvs.width = width;
                cvs.height = height;
            }
            var ctx = cvs.getContext("2d");
            ctx.clearRect(0, 0, cvs.width, cvs.height);
            ctx.drawImage(source_img_obj, 0, 0, cvs.width, cvs.height);
            var newImageData = cvs.toDataURL(mime_type, quality);
            var result_image_obj = new Image();
            result_image_obj.src = newImageData;
            return result_image_obj;
        }
    };

    function Selecttx(evt) {
        var files = evt.target.files;
        for (var i = 0, f; f = files[i]; i++) {
            // Only process image files.
            if (!f.type.match('image.*')) {
                continue;
            }

            var reader = new FileReader();
            // Closure to capture the file information.
            reader.onload = (function (theFile) {
                return function (e) {
                    var size = files[0].size;
                    var i = new Image();
                    var p1 = document.getElementById('img1');
                    var p2 = document.getElementById('img2');
                    var p3 = document.getElementById('img3');
                    var p4 = document.getElementById('img4');
                    var s = document.getElementById('imgsend');
                    i.src = e.target.result;
                    p1.src = jic.compress(i, size, 350, 350).src;
                    p2.src = p1.src;
                    p3.src = p1.src;
                    p4.src = p1.src;
                    s.value = p1.src;
                };
            })(f);
            // Read in the image file as a data URL.
            reader.readAsDataURL(f);
        }
        $('#settings-save4').removeAttr("disabled");
    }

    function Selectbg(evt) {
        var files = evt.target.files;
        for (var i = 0, f; f = files[i]; i++) {
            // Only process image files.
            if (!f.type.match('image.*')) {
                continue;
            }

            var reader = new FileReader();
            // Closure to capture the file information.
            reader.onload = (function (theFile) {
                return function (e) {
                    var size = files[0].size;
                    var i = new Image();
                    var b = document.getElementById('bg');
                    var s = document.getElementById('bgsend');
                    i.src = e.target.result;
                    b.src = jic.compress(i, size, 0, 0).src;
                    s.value = b.src;
                };
            })(f);
            // Read in the image file as a data URL.
            reader.readAsDataURL(f);
        }
        $('#settings-save5').removeAttr("disabled");
    }

    //判断文本框字数
    var personal = $('#personal').val();
    $("#personal").on("input", function () {
        var s = document.getElementById('screenflag');
        var p = document.getElementById('personalflag');
        var num = $(this).val().length;
        var text = $(this).val();
        if (num === 0 || num > 255) {
            p.value = 0;
            if (num > 255) {
                $("#personal-notification").html('超出字数限制，最多255字！');
                p.value = -1;
            } else {
                p.value = 0;
            }
            if (p.value + s.value <= 0) {
                $("#settings-save1").attr("disabled", "disabled");
            }
        } else if (text == personal) {
            p.value = 0;
            $("#personal-notification").empty();
            if (p.value + s.value <= 0) {
                $("#settings-save1").attr("disabled", "disabled");
            }
        } else {
            p.value = 1;
            $("#personal-notification").empty();
            if (p.value + s.value >= 1) {
                $("#settings-save1").removeAttr("disabled");
            }
        }
    });
    //判断screen-name
    var screenname = $('input[name="screen-name"]').val();
    $('#screen-name').on('input', function () {
            var s = document.getElementById('screenflag');
            var p = document.getElementById('personalflag');
            var name = $(this).val();
            if (name == '' || name == undefined) {
                s.value = 0;
                $('#screen-name-notification').html('昵称不能为空！');
                if (p.value + s.value <= 0) {
                    $("#settings-save1").attr("disabled", "disabled");
                }
            } else if (name == screenname) {
                s.value = 0;
                $('#screen-name-notification').empty();
                if (p.value + s.value <= 0) {
                    $("#settings-save1").attr("disabled", "disabled");
                }
            } else {
                s.value = 1;
                $('#screen-name-notification').empty();
                if (p.value + s.value >= 1) {
                    $("#settings-save1").removeAttr("disabled");
                }
            }
        }
    );
    /**
     * ajax to verify name || email
     * */
    var username = $('input[name="username"]').val();
    $('#username').on('input', function () {
        var u = document.getElementById('usernameflag');
        var e = document.getElementById('emailflag');
        var name = $(this).val();
        if (name == '' || name == undefined) {
            u.value = -1;
            $('#username-notification').html('用户名不能为空！');
            if (u.value + e.value <= 0) {
                $("#settings-save2").attr("disabled", "disabled");
            }
        } else if (name == username) {
            u.value = 0;
            $('#username-notification').empty();
            if (u.value + e.value <= 0) {
                $("#settings-save2").attr("disabled", "disabled");
            }
        } else {
            $.ajax({
                url: 'Settings/verifyname',
                type: 'post',
                data: {
                    name: name
                },
                error: function () {

                },
                success: function (result) {
                    if (result == 0) {
                        u.value = 1;
                        $('#username-notification').empty();
                        if (u.value + e.value >= 1) {
                            $("#settings-save2").removeAttr("disabled");
                        }
                    } else {
                        u.value = -1;
                        $('#username-notification').html('用户名已经存在，请使用新的用户名');
                        if (u.value + e.value >= 1) {
                            $("#settings-save2").attr("disabled", "disabled");
                        }
                    }
                }
            });
        }
    });
    var useremail = $('input[name="user-email"]').val();
    $('#user-email').on('input', function () {
        var u = document.getElementById('usernameflag');
        var e = document.getElementById('emailflag');
        var email = $(this).val();
        if (email == '' || email == undefined) {
            u.value = -1;
            $('#email-notification').html('邮箱不能为空！');
            if (u.value + e.value <= 0) {
                $("#settings-save2").attr("disabled", "disabled");
            }
        } else if (email == useremail) {
            u.value = 0;
            $('#email-notification').empty();
            if (u.value + e.value <= 0) {
                $("#settings-save2").attr("disabled", "disabled");
            }
        } else {
            $.ajax({
                url: 'Settings/verifyemail',
                type: 'post',
                data: {
                    email: email
                },
                error: function () {

                },
                success: function (result) {
                    if (result == 0) {
                        e.value = 1;
                        $('#email-notification').empty();
                        if (u.value + e.value >= 1) {
                            $("#settings-save2").removeAttr("disabled");
                        }
                    } else {
                        e.value = -1;
                        $('#email-notification').html('邮箱已经存在，请使用新的邮箱');
                        if (u.value + e.value >= 1) {
                            $("#settings-save2").attr("disabled", "disabled");
                        }
                    }
                }
            });
        }
    });

    $('#current-password').on('change', function () {
        var cp = document.getElementById('cp');
        var pwd = $(this).val();
        if (pwd == '') {
            cp.value = -1;
            $('#password-save3').attr('disabled', 'disabled');
            $('#current-password-notification').html('密码不能为空！请输入密码！');
        } else {
            $.ajax({
                url: 'Settings/verifypassword',
                type: 'post',
                data: {
                    pwd: pwd
                },
                success: function (result) {
                    if (result == 1) {
                        cp.value = 1;
                        $('#current-password-notification').empty();
                    } else {
                        cp.value = -1;
                        $('#password-save3').attr('disabled', 'disabled');
                        $('#current-password-notification').html('密码不匹配！请重新输入密码！');
                    }
                }
            });
        }
    });

    /**
     * 验证密码
     * */
    $('#userpassword').on('change', function () {
        var up = document.getElementById('up');
        var pwd = $(this).val();
        var cpwd = $('#current-password').val();
        if (pwd == '') {
            up.value = -1;
            $('#password-save3').attr('disabled', 'disabled');
            $('#userpassword-notification').html('新密码不能为空！请重新输入新密码！');
        } else if (pwd == cpwd) {
            up.value = -1;
            $('#password-save3').attr('disabled', 'disabled');
            $('#userpassword-notification').html('新密码不能和当前密码一致！请重新输入新密码！');
        } else {
            up.value = 1;
            $('#userpassword-notification').empty();
        }
    });
    $('#userpassword-verify').on('change', function () {
        var cp = document.getElementById('cp');
        var up = document.getElementById('up');
        var upv = document.getElementById('upv');
        var pwd = $(this).val();
        var vpwd = $('#userpassword').val();
        if (pwd == '') {
            upv.value = -1;
            $('#password-save3').attr('disabled', 'disabled');
            $('#userpassword-verify-notification').html('密码不能为空！请重新输入新密码！');
        } else if (pwd == vpwd) {
            upv.value = 1;
            $('#userpassword-verify-notification').empty();
            if (cp.value + up.value + upv.value > 2) {
                $('#password-save3').removeAttr('disabled');
            }
        } else {
            upv.value = -1;
            $('#password-save3').attr('disabled', 'disabled');
            $('#userpassword-verify-notification').html('密码不匹配！请重新输入！');
        }
    });

});